package com.ym.medical.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.base.enums.FlagEnum;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.medical.dao.PatientDetailDao;
import com.ym.medical.dao.PatientHealthRecordsDao;
import com.ym.medical.dao.PlateDao;
import com.ym.medical.dao.SysHelpDao;
import com.ym.medical.dao.UserDao;
import com.ym.medical.domain.entity.PatientDetailEntity;
import com.ym.medical.domain.entity.PatientHealthRecordsEntity;
import com.ym.medical.domain.entity.PlateEntity;
import com.ym.medical.domain.entity.SysHelpEntity;
import com.ym.medical.domain.param.AddQuestionerParam;
import com.ym.medical.domain.param.PlateParam;
import com.ym.medical.domain.param.SysHelpParam;
import com.ym.medical.domain.vo.DoctorVo;
import com.ym.medical.enums.PatientDetailRelationEnum;
import com.ym.medical.enums.PublisStatusEnum;
import com.ym.medical.enums.SystemGroupEnum;
import com.ym.medical.service.IPatientCenterSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author : 张文博
 * @date : 2019/5/14 13:41
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PatientCenterImpl implements IPatientCenterSV {

    @Autowired
    private PatientHealthRecordsDao patientHealthRecordsDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private SysHelpDao sysHelpDao;

    @Autowired
    private PlateDao plateDao;

    @Autowired
    private PatientDetailDao patientDetailDao;




    /**
     * 患者健康档案
     *
     * @param patientId
     * @return
     */
    @Override
    public PatientHealthRecordsEntity selectArchives(Long patientId) {
        PatientHealthRecordsEntity patientHealthRecords =
                patientHealthRecordsDao.selectArchives(patientId);
        return patientHealthRecords;
    }

    /***
     * 患者关注的医生
     * @param currUserId
     * @return
     */
    @Override
    public PageInfo<DoctorVo> selectMyDoctor(int pagrNo, int limit, Long currUserId) {
        PageHelper.startPage(pagrNo, limit);
        List<DoctorVo> myFocus = userDao.selectMyDoctor(
                currUserId,
                SystemGroupEnum.PATIENT.FANS.getName(),
                SystemGroupEnum.PATIENT.INQUIRY.getName());
        PageInfo<DoctorVo> doctorVoPageInfo = new PageInfo<>(myFocus);
        return doctorVoPageInfo;
    }

    /***
     * 患者健康档案添加
     * @param record
     * @return
     */
    @Override
    public PatientHealthRecordsEntity addArchives(PatientHealthRecordsEntity record) {
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        patientHealthRecordsDao.insertSelective(record);
        return record;
    }

    /***
     * 家人健康档案修改
     * @param patientHealthRecords
     * @return
     */
    @Override
    public int upArchives(PatientHealthRecordsEntity patientHealthRecords) {
        int rows = patientHealthRecordsDao.updateByPrimaryKeySelective(patientHealthRecords);
        return rows;
    }

    /***
     * 帮助信息查询
     * @param pageNo
     * @param limit
     * @param sysHelpModel
     * @return
     */
    @Override
    public PageInfo<SysHelpEntity> selectHelpInfo(int pageNo, int limit, SysHelpParam sysHelpModel) {
        PageHelper.startPage(pageNo, limit);
        sysHelpModel.setReleaseState(PublisStatusEnum.OVER_PUBLISH.getKey());
        List<SysHelpEntity> list = sysHelpDao.select(sysHelpModel.covert());
        PageInfo<SysHelpEntity> sysHelpPageInfo = new PageInfo<>(list);
        return sysHelpPageInfo;
    }

    /***
     * 板块列表查询
     * @param pageNo
     * @param limit
     * @param plateModel
     * @return
     */
    @Override
    public PageInfo<PlateEntity> selectMyPlate(int pageNo, int limit, PlateParam plateModel) {
        PageHelper.startPage(pageNo, limit);
        plateModel.setState("01");
        List<PlateEntity> plateList = plateDao.select(plateModel.covert());
        PageInfo<PlateEntity> pageInfo = new PageInfo<>(plateList);
        return pageInfo;
    }

    /**
     * 用户选择的板块
     *
     * @param userId
     * @return
     */
    @Override
    public List<PlateEntity> selectUserPlate(Long userId) {
        List<PlateEntity> userPlate = plateDao.selectUserPlate(userId);
        return userPlate;
    }

    /***
     * 用户板块添加
     * @param plateIds
     * @param userId
     * @return
     */
    @Override
    public int addPlate(Long[] plateIds, Long userId) {
        int rows = plateDao.addPlate(plateIds, userId);
        return rows;
    }

    /***
     * 用户板块删除
     * @param plateIds
     * @param userId
     * @return
     */
    @Override
    public int deleteMyPlate(Long[] plateIds, Long userId) {
        int rows = plateDao.deleteMyPlate(plateIds, userId);
        return rows;
    }

    /***
     * 添加好友
     * @param patientDetail
     * @return
     */
    @Override
    public Long addOthers(AddQuestionerParam patientDetail, Long userId) {
        //查询是否已经存在,身份证信息校验
        PatientDetailEntity patientDetail2 = patientDetail.getPatientDetail();
        if (patientDetail2.getIdNo() != null && !"".equals(patientDetail2.getIdNo())){
            String idNO = patientDetail2.getIdNo();
            PatientDetailEntity m = PatientDetailEntity.builder().userId(userId).idNo(idNO).build();
            PatientDetailEntity p = patientDetailDao.selectOne(m);
            if (p != null) {
                throw new BusinessException("身份证号已存在");
            }
        }

        //查询是否有本人关系存在
        if (PatientDetailRelationEnum.I_AM.getKey().equals(patientDetail2.getRelation())){
            PatientDetailEntity model = PatientDetailEntity.builder()
                    .relation(PatientDetailRelationEnum.I_AM.getKey()).userId(patientDetail2.getUserId()).build();
            PatientDetailEntity p2 = patientDetailDao.selectOne(model);
            if (p2 != null) {
                throw new BusinessException("请选择其他亲属关系");
            }
        }
        patientDetail2.setCreateTime(new Date());
        patientDetail2.setUpdateTime(new Date());
        patientDetail2.setDeleted("00");
        int rows = patientDetailDao.insertSelective(patientDetail2);
        PatientHealthRecordsEntity patientHealthRecords = patientDetail.getPatientHealthRecords();
        if (null == patientHealthRecords){
            return patientDetail2.getId();
        }else {
            patientHealthRecords.setPatientId(patientDetail2.getId());
            addArchives(patientHealthRecords);
        }
        return patientDetail2.getId();

    }

    /***
     * 删除家人
     * @param patientId
     * @return
     */
    @Override
    public int deletePerson(Long patientId) {
        PatientDetailEntity patientDetail = new PatientDetailEntity();
        patientDetail.setId(patientId);
        patientDetail.setDeleted(FlagEnum.IS.getKey());
        int rows = patientDetailDao.updateByPrimaryKeySelective(patientDetail);
        return rows;
    }



    /***
     * 患者家人信息修改
     * @param patientDetail
     * @return
     */
    @Override
    public int updatePatient(PatientDetailEntity patientDetail) {
        //查询是否已经存在,身份证信息校验
        String idNO = null;
        Long patientId = patientDetail.getId();
        if (patientDetail.getIdNo() != null && !"".equals(patientDetail.getIdNo())){
            idNO = patientDetail.getIdNo();
            PatientDetailEntity p = patientDetailDao.selectOneByExample(
                    TK.selectWhere(PatientDetailEntity.class)
                    .andEqualTo(PatientDetailEntity::getUserId,patientDetail.getUserId())
                    .andNotEqualTo(PatientDetailEntity::getId,patientId)
                    .andEqualTo(PatientDetailEntity::getIdNo,idNO)
                    .end()
            );
            if (p != null) {
                throw new BusinessException("身份证号已存在");
            }
        }

        //查询是否有本人关系存在
        if (PatientDetailRelationEnum.I_AM.getKey().equals(patientDetail.getRelation())){
            PatientDetailEntity p2 = patientDetailDao.selectOneByExample(
                    TK.selectWhere(PatientDetailEntity.class)
                    .andEqualTo(PatientDetailEntity::getUserId,patientDetail.getUserId())
                    .andEqualTo(PatientDetailEntity::getRelation,"01")
                    .andNotEqualTo(PatientDetailEntity::getId,patientId)
                    .end()
            );
            if (p2 != null) {
                throw new BusinessException("请选择其他亲属关系");
            }
        }
        patientDetail.setUpdateTime(new Date());
        int rows = patientDetailDao.updateByPrimaryKeySelective(patientDetail);
        return rows;
    }


}
